// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var base = require("base");
	var databind = require("databind");
	var tools = require("tools");
	var type;
	var datas1;
	var datas2;
	var datas3;
	var datas4;
	var typeNum;
	var text = "收付款动态";
	var tpl = $(".dataTpl1").html();

	var init = function() {
		type = base.getUrlParam("type");
		if(type == 1) {
			typeNum = 2;
			$("h3").html("当前贷款发放");
			tpl = $(".dataTpl1").html();
		} else if(type == 2) {
			typeNum = 1;
			$("h3").html("累计贷款发放");
			tpl = $(".dataTpl1").html();
		} else if(type == 3) {
			typeNum = 1;
			$("h3").html("累计还款");
			tpl = $(".dataTpl2").html();
		} else if (type == 4) {
			typeNum = 1;
			$("h3").html("贷款利息收取报表");
			tpl = $(".dataTpl3").html();
		} else if (type == 5) {
			typeNum = 2;
			$("h3").html("当前还款");
			tpl = $(".dataTpl2").html();
		} else if (type == 6) {
			typeNum = 3;
			$("h3").html("当日还款");
			text = "收付款动态(近七日)"
			tpl = $(".dataTpl2").html();
		} else if (type == 7) {
			typeNum = 3;
			$("h3").html("当日放款");
			text = "收付款动态(近七日)";
			tpl = $(".dataTpl1").html();
		}
		inqurieReceivePaymentDetail(typeNum);
		inqurieReceivePaymentList(typeNum);
		initEvent();
	};
	var initEvent = function() {};
	
	var inqurieReceivePaymentList = function(typeNum) {
		base.fnAjax({
			action: "inqurieReceivePaymentList.json",
			async: true,
			nocode: true,
			oData: {
				type: typeNum
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					var html = juicer(tpl, data);
					$(".dataBody").html(html);
					
					$('.dataBody').stickySort({ 
						sortable: true
					});
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var inqurieReceivePaymentDetail = function(typeNum) {
		base.fnAjax({
			action: "inqurieReceivePaymentDetail.json",
			async: true,
			nocode: true,
			oData: {
				type: typeNum
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					datas1 = data.datas[0].dates;
					datas2 = data.datas[0].receives;
					datas3 = data.datas[0].payments;
					datas4 = data.datas[0].netInflows;
					showChart();
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var showChart = function() {
		// 基于准备好的dom，初始化echarts图表
		var myChart0 = echarts.init(document.getElementById('main0'));
		//var app.title = '折柱混合';

		var option0 = {
			title: {
				text: text,
				subtext: '一润供应链',
				x: 'left'
			},
			tooltip: {
				trigger: 'axis'
			},
			toolbox: {
				feature: {
					dataView: {
						show: true,
						readOnly: false
					},
					magicType: {
						show: true,
						type: ['line', 'bar']
					},
					restore: {
						show: true
					},
					saveAsImage: {
						show: true
					}
				}
			},
			legend: {
				x: 'center',
				y: 'bottom',
				data: ['收款', '还款', '净流入']
			},
			grid: {
				left: '3%',
				right: '4%',
				bottom: '3%',
				containLabel: true
			},
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: datas1
			}],
			yAxis: [{
				type: 'value'
			}],
			series: [{
				name: '收款',
				type: 'bar',
				data: datas2
			}, {
				name: '放款',
				type: 'bar',
				data: datas3
			}, {
				name: '净流入',
				type: 'line',
				stack: '总量',
				label: {
					normal: {
						show: true,
						position: 'top'
					}
				},
				areaStyle: {
					normal: {}
				},
				data: datas4
			}]
		};
		// 使用刚指定的配置项和数据显示图表。
		myChart0.setOption(option0);
	};

	return {
		init: init
	};
});